﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;

namespace MultyLanguage
{
    public class XMLUltility
    {
        public static string getValueByID(string element, string id, string xmlpath)
        {
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(xmlpath);
            XmlNodeList nlist = xdoc.GetElementsByTagName(element);
            for (int i = 0; i < nlist.Count; i++)
            {
                XmlAttributeCollection  attlist = nlist.Item(i).Attributes;
                for (int j = 0; j < attlist.Count ; j++)
                {
                    if (attlist[j].Value.ToLower().Trim() == id.ToLower().Trim())
                        return  nlist.Item(i).ChildNodes[0].Value;
                }
            }
            return " ";
        }
        public static string SetOrEditValue(string element, string id, string val, string xmlpath)
        {
            try
            {
                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(xmlpath);
                XmlNodeList nlist = xdoc.GetElementsByTagName(element);
                for (int i = 0; i < nlist.Count; i++)
                {
                    XmlAttributeCollection attlist = nlist.Item(i).Attributes;
                    for (int j = 0; j < attlist.Count; j++)
                    {
                        if (attlist[j].Value.ToLower().Trim() == id.ToLower().Trim())
                        {
                            nlist.Item(i).ChildNodes[0].Value = val;
                            xdoc.Save(xmlpath);
                            return "Success";
                        }
                    }
                }
                // when this control does not exist
                string node = @"<" + element  + @" id=""" + id + @"""> " + val + " </" + element +">";

                //XmlNode xnode =  xdoc.CreateElement(element);
                xdoc.DocumentElement.InnerXml += node;
                //xdoc.DocumentElement.AppendChild(xnode);
                xdoc.Save(xmlpath);
                return "Success";
            }
            catch (Exception ex)
            {
                return "Fail";
            }
        }
        public static bool SetValue(string element, string id, string val, string xmlpath)
        {
            try
            {
                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(xmlpath);
                XmlNodeList nlist = xdoc.GetElementsByTagName(element);
                for (int i = 0; i < nlist.Count; i++)
                {
                    XmlAttributeCollection attlist = nlist.Item(i).Attributes;
                    for (int j = 0; j < attlist.Count; j++)
                    {
                        if (attlist[j].Value.ToLower().Trim() == id.ToLower().Trim())
                        {
                            return false ;
                        }
                    }
                }
                // when this control does not exist
                string node = @"<" + element  + @" id=""" + id + @"""> " + val + " </" + element  + ">";
                //XmlNode xnode =  xdoc.CreateElement(element);
                xdoc.DocumentElement.InnerXml += node;
                //xdoc.DocumentElement.AppendChild(xnode);
                xdoc.Save(xmlpath);
                return true;
                 
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
